package io.osw.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;

import io.osw.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.osw.modules.sys.entity.TblBudgetEntity;
import io.osw.modules.sys.service.TblBudgetService;
import io.osw.common.utils.PageUtils;
import io.osw.common.utils.R;
import java.util.Date;


/**
 * 预算表
 *
 * @author guxiaoguang
 * @email xuebai864627555@gmail.com
 * @date 2019-01-08 22:53:53
 */
@RestController
@RequestMapping("sys/tblbudget")
public class TblBudgetController extends AbstractController{
    @Autowired
    private TblBudgetService tblBudgetService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:tblbudget:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tblBudgetService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{budgetId}")
    @RequiresPermissions("sys:tblbudget:info")
    public R info(@PathVariable("budgetId") Long budgetId){
        TblBudgetEntity tblBudget = tblBudgetService.selectById(budgetId);

        return R.ok().put("tblBudget", tblBudget);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:tblbudget:save")
    public R save(@RequestBody TblBudgetEntity tblBudget){
        tblBudget.setCreateTime(new Date());
        tblBudget.setCreateBy(getUser().getUserId());
        tblBudgetService.insert(tblBudget);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:tblbudget:update")
    public R update(@RequestBody TblBudgetEntity tblBudget){
        ValidatorUtils.validateEntity(tblBudget);
        tblBudget.setUpdateTime(new Date());
        tblBudget.setUpdateBy(getUser().getUserId());
        tblBudgetService.updateAllColumnById(tblBudget);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:tblbudget:delete")
    public R delete(@RequestBody Long[] budgetIds){
        tblBudgetService.deleteBatchIds(Arrays.asList(budgetIds));

        return R.ok();
    }

}
